<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .box{width: 700px;height: 600px;border: solid 1px black;position: relative;margin: 0 auto;}
    .xbox{width: 100px;height: 100px;background: red;position: absolute;left:0;top:0;border-radius: 50%;}
  </style>
</head>
<body>
  <div class="box">
    <div class="xbox"></div>
  </div>
</body>
<script>
  const xbox = document.querySelector(".xbox");

  let t;
  const target = 500;
  let speed = 5;

  let speedX = 10;
  const targetRight = 600;

  document.onclick = function(){
    if(t) return;
    t = setInterval(()=>{

      // 下方目标
      if(target - xbox.offsetTop < speed ){
        xbox.style.top = target + "px";
        speed = -speed;
      }else{
        xbox.style.top = xbox.offsetTop + speed/2 + "px";
      }

      // 上方目标
      if(xbox.offsetTop < 0 ){
        xbox.style.top = 0 + "px";
        speed = -speed;
      }else{
        xbox.style.top = xbox.offsetTop + speed/2 + "px";
      }

      // 右侧目标
      if(targetRight - xbox.offsetLeft < speedX ){
        xbox.style.left = targetRight + "px";
        speedX = -speedX;
      }else{
        xbox.style.left = xbox.offsetLeft + speedX/2 + "px";
      }

      // 左侧目标
      if(xbox.offsetLeft < 0 ){
        xbox.style.left = 0 + "px";
        speedX = -speedX;
      }else{
        xbox.style.left = xbox.offsetLeft + speedX/2 + "px";
      }


    }, 30);
  }

</script>
</html>